package hander

import (
	"github.com/gin-gonic/gin"
	"good/api/reponse"
	"good/api/request"
	"good/global"
	"math/rand"
	"time"
)

func SendSms(c *gin.Context) {
	req := request.SendSms{}
	if err := c.ShouldBind(&req); err != nil {
		reponse.ErrorRequest(c, "绑定失败")
		return
	}
	get := global.Rdb.Get(global.Ctx, "sendSms"+req.Phone)
	if get.Val() > "1" {
		reponse.ErrorRequest(c, "操作频繁,请一分钟后再试")
		return
	}
	//if req.Phone > strconv.Itoa(11) {
	//	reponse.ErrorRequest(c, "手机号码不能超过11位")
	//	return
	//}
	code := rand.Intn(9000) + 1000
	//sms, err := pkg.SendSms(req.Phone, strconv.Itoa(code))
	//if err != nil {
	//	return
	//}
	//if *sms.Body.Code != "OK" {
	//	reponse.ErrorRequest(c, *sms.Body.Message)
	//}
	global.Rdb.Set(global.Ctx, "sendSms"+req.Phone, code, time.Minute*5)
	global.Rdb.Incr(global.Ctx, "sendSms"+req.Phone)
	global.Rdb.Expire(global.Ctx, "sendSms"+req.Phone, time.Minute*5)
	reponse.SuccessRequest(c, "发送成功", req.Phone)
}
